package persistence

import (
	"Data-API-MCP/backend/internal/domain/entity"
	"context"
)

// DBService 数据库服务
type DBService struct {
	dbPool *DBPool
}

// NewDBService 创建数据库服务
func NewDBService(dbPool *DBPool) *DBService {
	return &DBService{
		dbPool: dbPool,
	}
}

// CreateConnection 创建数据库连接
func (s *DBService) CreateConnection(ctx context.Context, conn *entity.DBConnection) error {
	return s.dbPool.CreateConnection(ctx, conn)
}

// GetConnections 获取数据库连接列表
func (s *DBService) GetConnections(ctx context.Context) ([]*entity.DBConnection, error) {
	return s.dbPool.GetConnections(ctx)
}

// GetConnection 获取单个数据库连接
func (s *DBService) GetConnection(ctx context.Context, id int64) (*entity.DBConnection, error) {
	return s.dbPool.GetConnection(ctx, id)
}

// UpdateConnection 更新数据库连接
func (s *DBService) UpdateConnection(ctx context.Context, conn *entity.DBConnection) error {
	return s.dbPool.UpdateConnection(ctx, conn)
}

// DeleteConnection 删除数据库连接
func (s *DBService) DeleteConnection(ctx context.Context, id int64) error {
	return s.dbPool.DeleteConnection(ctx, id)
}

// TestConnection 测试数据库连接
func (s *DBService) TestConnection(ctx context.Context, conn *entity.DBConnection) error {
	return s.dbPool.TestConnection(ctx, conn)
}

// GetTables 获取表列表
func (s *DBService) GetTables(ctx context.Context, id int64) ([]*entity.TableInfo, error) {
	return s.dbPool.GetTables(ctx, id)
}

// GetTableInfo 获取表信息
func (s *DBService) GetTableInfo(ctx context.Context, id int64, tableName string) (*entity.TableInfo, error) {
	return s.dbPool.GetTableInfo(ctx, id, tableName)
}

// GetColumns 获取列信息
func (s *DBService) GetColumns(ctx context.Context, id int64, tableName string) ([]*entity.ColumnInfo, error) {
	return s.dbPool.GetColumns(ctx, id, tableName)
}

// GetTableData 获取表数据
func (s *DBService) GetTableData(ctx context.Context, id int64, tableName string, page, pageSize int) (*entity.QueryResult, error) {
	return s.dbPool.GetTableData(ctx, id, tableName, page, pageSize)
}

// ExecuteQuery 执行查询
func (s *DBService) ExecuteQuery(ctx context.Context, id int64, query string) (*entity.QueryResult, error) {
	return s.dbPool.ExecuteQuery(ctx, id, query)
}
